package com.contactmgr.activity;

import android.app.ListActivity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;

import com.contactmgr.R;

public class GestureTest extends TabActivity implements OnGestureListener {
	private TabHost tabHost;
	private GestureDetector gestureScanner;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.testmainlayoutforgesture);
		gestureScanner = new GestureDetector(this);
		addTabs();
	}

	private void addTabs() {
		Intent i1 = new Intent().setClass(this, SimpleListActivity.class);
		Intent i2 = new Intent().setClass(this, SimpleActivity.class);
		Intent i3 = new Intent().setClass(this, SimpleActivity.class);
		
		tabHost = getTabHost();
		tabHost.addTab(tabHost.newTabSpec("First").setIndicator("First").setContent(i1));
		tabHost.addTab(tabHost.newTabSpec("Second").setIndicator("Second").setContent(i2));
		tabHost.addTab(tabHost.newTabSpec("Third").setIndicator("Third").setContent(i3));
		
		tabHost.setCurrentTab(0);
	}
	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
	    if (gestureScanner != null) {
	        if (gestureScanner.onTouchEvent(ev))
	            return true;
	    }
	    return super.dispatchTouchEvent(ev);
	}
	@Override
	public boolean onTouchEvent(MotionEvent me) {
		return gestureScanner.onTouchEvent(me);
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

		System.out.println("1 \t" + e1.getX() + "," + e1.getY());
		System.out.println("2 \t" + e2.getX() + "," + e2.getY());
		System.out.println("VelocityX :" + velocityX);
		System.out.println("VelocityY :" + velocityY);
		float dispX = e2.getX() - e1.getX();
		float dispY = e2.getY() - e1.getY();
		if (Math.abs(dispX) >= 200 && Math.abs(dispY) <= 100) {
			// swipe ok
			if (dispX > 0) {
				// L-R swipe
				System.out.println("L-R swipe");
				changeLtoR();
			} else {
				// R-L swipe
				System.out.println("R-L swipe");
			}
		}
		return true;
	}

	private void changeLtoR() {
		int curTab = tabHost.getCurrentTab();
		int noOfTabs = getTabHost().getTabWidget().getTabCount();
		int nextTab = ((curTab + 1) % noOfTabs);
		tabHost.setCurrentTab(nextTab);

	}

	@Override
	public boolean onDown(MotionEvent e) {
		return true;
	}

	@Override
	public void onLongPress(MotionEvent e) {
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
		return true;
	}

	@Override
	public void onShowPress(MotionEvent e) {
	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		return true;
	}

}
